﻿namespace sLight.Views.User
{
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using sLightLibrary.BasicApp.Views.Forms.User;
    using DevExpress.Xpf.NavBar;
    using DevExpress.Xpf.Core.WPFCompatibility;
    using System.Text;
    using System.Collections;
    using DevExpress.Xpf.Editors;
    using System.Windows.Input;
    using DevExpress.Xpf.Data;
    using sLight.Web;
    using System.ServiceModel.DomainServices.Client;

    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class Home : Page
    {
        /// <summary>
        /// Creates a new <see cref="Home"/> instance.
        /// </summary>
        private sLightContext datacontext = new sLightContext();
        public Home()
        {
            InitializeComponent();

            this.Title = ApplicationStrings.HomePageTitle;
            navBar.AddHandler(NavBarControl.MouseLeftButtonDownEvent, new MouseButtonEventHandler(navBar_MouseLeftButtonDown), true);
            LoadOperation<Order> loadOperation = datacontext.Load(datacontext.GetOrderQuery());
            orderlist.DataSource = loadOperation.Entities;
            gridView.AddHandler(Grid.MouseLeftButtonDownEvent, new MouseButtonEventHandler(gridView_MouseLeftButtonDown), true);
        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void navBar_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            NavBarGroup group = navBar.View.GetNavBarGroup(e);
            NavBarItem item = navBar.View.GetNavBarItem(e);
           
            if (group != null && item != null)
                if (item.Content.Equals("Order (click for test the FormBase)"))
                {
                    orderlist.Visibility = System.Windows.Visibility.Visible;
                    
                  
                }
                else
                {
                    orderlist.Visibility = System.Windows.Visibility.Collapsed;
                    if (item.Content.Equals("Customer List(click for test the ListBase)"))
                    {
                        ListPage lb = new ListPage();
                        lb.Show();
                    }
                }
        }

        private void gridView_SelectionChanged(object sender, DevExpress.Xpf.Grid.GridSelectionChangedEventArgs e)
        {
            string text = orderlist.GetFocusedRowCellValue("Name").ToString();
        }

        private void gridView_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (orderlist.GetFocusedRow() != null)
            {
                string text = orderlist.GetFocusedRowCellValue("Name").ToString();
                OrderPage orderpage = new OrderPage();
                orderpage.Title = text;
                orderpage.Show();
               
            }
            
        }
       
    }
}